import axios from 'axios'
import Vue from 'vue'
import Vuex,{Store} from 'vuex'

Vue.use(Vuex)

export default new Store({
  actions:{
    addPerson({state,commit},value){
      if(state.personList.length < 10){
        commit('ADD_PERSON',value)
      }else{
        alert('最大人数为10')
      }
    },
    randomPicture({commit}){
      // axios.get('https://dog.ceo/api/breed/corgi/cardigan/images/random').then(
      axios.get('https://api.uomg.com/api/rand.img3?sort=胖次猫&format=json').then(
        response => {
          commit('ADD_PICTURE',response.data.imgurl)
        },
        error => {
          alert(error.message)
        }
      )
    }
  },
  mutations:{
    ADD_PERSON(state,value){
      state.personList.unshift(value)
    },
    ADD_PICTURE(state,value){
      state.imgList.push(value)
    }
  },
  state:{
    personList:[
      {id:'aoisyd76ardf01',name:'海峰',age:20},
      {id:'aoisyd76ardf02',name:'程程',age:30},
      {id:'aoisyd76ardf03',name:'晶晶',age:90}
    ],
    imgList:[
      'http://49.232.112.44/images/hot.jpg',
      'http://49.232.112.44/images/cool.jpg'
    ]
  }
})